package com.shaonianyou.controller;

import java.security.Principal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.collect.Maps;
import com.shaonianyou.common.HttpClient;
import com.shaonianyou.util.Pagination;

@Controller
public class BaseController {

	@Autowired
	HttpClient httpClient;

	@Value("${wechat.app.appId}")
	String appId;

	@Value("${wechat.app.appSecret}")
	String appSecret;

	/**
	 * POST请求
	 * 
	 * @param url
	 * @param param
	 * @param responseType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	<T> T doPost(String url, Map<String, Object> param, Class<T> responseType) {
		Map<String, Object> result = (Map<String, Object>) httpClient.postForObject(url, param, responseType);
		return (T) result;
	}

	/**
	 * POST请求 返回Map
	 * 
	 * @param url
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> doPost(String url, Map<String, Object> param) {
		if (null != param.get("pageIndex")) {
			Pagination page = Pagination.getInstance(Integer.valueOf(param.get("pageIndex").toString()));
			param.put("start", page.getStart());
			param.put("length", page.getLength());
		}
		// if(null == param.get("custId")){param.put("custId", "13");}
		return httpClient.postForObject(url, param, Map.class);
	}

	/**
	 * GET请求
	 * 
	 * @param url
	 * @param param
	 * @param responseType
	 * @return
	 */
	<T> T doGet(String url, Map<String, Object> param, Class<T> responseType) {
		return httpClient.getForObject(url, responseType, param);
	}

	/**
	 * GET请求返回Map
	 * 
	 * @param url
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	Map<String, Object> doGet(String url, Map<String, Object> param) {
		return httpClient.getForObject(url, Map.class, param);
	}

	public static Principal getLoginUser() {
		Authentication au = SecurityContextHolder.getContext().getAuthentication();
		Principal user = (Principal) au.getPrincipal();
		return user;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/checkOpenId/{code}", method = RequestMethod.POST)
	Map<String, Object> checkOpenId(@PathVariable String code) {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret
				+ "&code=" + code + "&grant_type=authorization_code";
		Map<String, Object> result = httpClient.getForObject(url, Map.class);
		String openid = String.valueOf(result.get("openid"));
		String access_token = String.valueOf(result.get("access_token"));
		Map<String, Object> param = Maps.newHashMap();
		param.put("openid", openid);
		param.put("access_token", access_token);
		Map<String, Object> resultMap = doPost("/findCustByOpenid", param);
		resultMap.put("openid", openid);
		return resultMap;
	}

}
